﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using LocalOverride.Fiddler.Store;

namespace LocalOverride.Fiddler.UI
{
    public partial class MappingEditor : UserControl, IMappingEditorView, ILoggingView
    {

        public MappingEditor()
        {
            InitializeComponent();

            mappingGridView.AutoGenerateColumns = false;

            editMappingControl.SaveEdit += editMappingControl_SaveEdit;
            editMappingControl.CancelEdit += editMappingControl_CancelEdit;
        }

        void editMappingControl_SaveEdit(object sender, EventArgs e)
        {
            OnSaveOverrideEdit();
        }

        void editMappingControl_CancelEdit(object sender, EventArgs args)
        {
            OnCancelOverrideEdit();
        }

        private void btnAddOverride_Click(object sender, EventArgs e)
        {
            OnAddOverride();
        }

        private void mappingGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (mappingGridView.Columns.Count > e.ColumnIndex)
                if (mappingGridView.Rows.Count > e.RowIndex)
                {
                    var item = (MappingEntry)mappingGridView.Rows[e.RowIndex].DataBoundItem;
                    string commandName = mappingGridView.Columns[e.ColumnIndex].Name;
                    if (commandName == "Delete")
                    {
                        OnDeleteOverride(item);
                    }
                    else if (commandName == "Edit")
                    {
                        OnEditOverride(item);
                    }
                }
        }

        public event EventHandler<EventArgs> AddOverride;
        public event EventHandler<EditOverrideEventArgs> EditOverride;
        public event EventHandler<DeleteOverrideEventArgs> DeleteOverride;

        public void OnDeleteOverride(MappingEntry mapping)
        {
            var args = new DeleteOverrideEventArgs(mapping);
            EventHandler<DeleteOverrideEventArgs> handler = DeleteOverride;
            if (handler != null) handler(this, args);
        }

        public void OnEditOverride(MappingEntry mapping)
        {
            var args = new EditOverrideEventArgs(mapping);
            EventHandler<EditOverrideEventArgs> handler = EditOverride;
            if (handler != null) handler(this, args);
        }

        public event EventHandler<EventArgs> CancelOverrideEdit;
        public event EventHandler<EventArgs> SaveOverrideEdit;

        public void OnSaveOverrideEdit()
        {
            EventHandler<EventArgs> handler = SaveOverrideEdit;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public void OnCancelOverrideEdit()
        {
            EventHandler<EventArgs> handler = CancelOverrideEdit;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public void OnAddOverride()
        {
            EventHandler<EventArgs> handler = AddOverride;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public string EditLocalPath
        {
            get { return editMappingControl.LocalPath; }
            set { editMappingControl.LocalPath = value; }
        }

        public string EditDefaultFileName
        {
            get { return editMappingControl.DefaultFileName; }
            set { editMappingControl.DefaultFileName = value; }
        }

        public string EditExtensions
        {
            get { return editMappingControl.Extensions; }
            set { editMappingControl.Extensions = value; }
        }

        public bool DownloadLocalCopy
        {
            get { return editMappingControl.DownloadLocalCopy; }
            set { editMappingControl.DownloadLocalCopy = value; }
        }

        public bool IsDirectory
        {
            get { return editMappingControl.IsDirectory; }
            set { editMappingControl.IsDirectory = value; }
        }

        private string logMessages = String.Empty;
        public string LogMessages
        {
            set
            {
                logMessages = value;
                Action updateLogMessages = UpdateLogMessages;
                Invoke(updateLogMessages);
            }
        }

        private void UpdateLogMessages()
        {
            txtLog.Text = logMessages;
            txtLog.SelectionStart = txtLog.Text.Length;
            txtLog.SelectionLength = 0;
            txtLog.ScrollToCaret();
            return;
        }

        public void ShowOverrideEditor()
        {
            tableLayoutPanel.RowStyles[0] = new RowStyle(SizeType.Absolute, 195);
        }

        public void HideOverrideEditor()
        {
            tableLayoutPanel.RowStyles[0] = new RowStyle(SizeType.Absolute, 0);
        }

        public void SetErrors(IEnumerable<ErrorNotification> errorNotification)
        {
            editMappingControl.SetErrors(errorNotification);
        }

        public void ClearErrors()
        {
            editMappingControl.ClearErrors();
        }

        public BindingList<MappingEntry> MappingList { set { mappingGridView.DataSource = value; } }

        public string EditUrl
        {
            get { return editMappingControl.Url; }
            set { editMappingControl.Url = value; }
        }
    }
}
